<?php
class FlinkAction extends CommonAction {

    public function index() {
        $fl = D('Flink');
        //搜索模块
        parent::search(array(
            'name'=>'链接名称',
            'url'=>'链接URL'
        ));
        if(!isset($_GET['act'])) {
            parent::page($fl->count());
            $allFlink = $fl->field(array('id', 'name', 'url', 'is_active', 'date'))
                           ->order('is_active DESC,date DESC')
                           ->limit($this->limit)
                           ->select();
        } else if($_GET['act'] == 'search') {
            parent::page($fl->where($this->map)->count());
            $allFlink = $fl->field(array('id', 'name', 'url', 'is_active', 'date'))
                           ->order('is_active DESC,date DESC')
                           ->where($this->map)
                           ->limit($this->limit)
                           ->select();
        }
        $this->assign(array(
            'empty'=>'<tr><td colspan="6">没有任何数据</td></tr>',
            'allflink'=>$allFlink
        ));
        $this->display();
    }

    public function add() {
        if(isset($_POST['send'])) {
            $fl = D('Flink');
            if($fl->create()) {
                $fl->add() ? $this->success('添加友情链接成功!', __URL__) : $this->error('系统出错，友情链接添加失败!', 0);
            } else {
                $this->error($fl->getError(), 0);
            }
        }
        $this->display();
    }

    public function update() {
        $id = $this->_get('id');
        if(isset($id)) {
            $fl = D('Flink');
            if(isset($_POST['send'])) {
                if($fl->create()) {
                    $fl->save() ? $this->success('修改友情链接成功!', __URL__) : $this->error('系统出错，修改友情链接失败!', 0);
                } else {
                    $this->error($fl->getError());
                }
            }
            if(!$fl->field('id')->where('id='.$id)->find()) $this->error('不存在此数据!', 0);
            $flink = $fl->field(array('id', 'name', 'url', 'is_active'))->where('id='.$id)->find();
            $this->assign('oneflink', $flink);
            $this->display();
        }

    }

    public function delete() {
        $id = $this->_get('id');
        if(isset($id)) {
            $fl = D('Flink');
            if(!$fl->where('id='.$id)->getField('id')) $this->error('不存在此数据!', 0);
            $fl->where('id='.$id)->delete() ? $this->success('删除友情链接成功!', __URL__) : $this->error('系统出错，删除友情链接失败!', 0);
        }
    }

    public function mulDelete() {
        $fl = D('Flink');
        $ids = implode(',', $_POST['select']);
        if(!$ids) $this->error('请先勾选一个选项!', 0);
        $map['id'] = array('in', $ids);
        $fl->where($map)->delete() ? $this->success('批量删除友情链接成功!', __URL__) : $this->error('系统出错，批量删除友情链接失败!', 0);
    }
}